﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.IBusiness.Report;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Coldairarrow.IBusiness;
using Coldairarrow.Entity.DTO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace Coldairarrow.Api.Controllers.Report
{
    /// <summary>
    /// 首页控制器
    /// </summary>
    [Route("/Report/[controller]/[action]")]
    public class MaterialSummaryController : BaseApiController
    {
        readonly IReport_MaterialSummaryBussiness _reportSvc;
        public MaterialSummaryController(IReport_MaterialSummaryBussiness svc)
        {
            _reportSvc = svc;
        }
        [HttpPost]
        [NoCheckJWT]
        public async Task<PageResult<Report_MaterialSummaryVM>> GetDataList(PageInput<Report_MaterialSummaryQM> input)
        {
            return await _reportSvc.GetDataListAsync(input);
        }
        /// <summary>
        /// 获取物料统计数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        [NoCheckJWT]
        public async Task<PageResultData<MaterialStatistics>> GetMaterialStatisticsList(PageInput<MaterialStatisticsSeach> input) {
            var Data = await _reportSvc.GetMaterialStatisticsList(input);
            return Data;
        }
        [HttpGet]
        /// <summary>
        /// 物料统计导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetExportList(string typeName,string Name)
        {

            PageInput<MaterialStatisticsSeach> input = new PageInput<MaterialStatisticsSeach>();
            input.Search.TypeName = typeName;
            input.Search.Name = Name;
            var datalist = await _reportSvc.GetMaterialStatisticsList(input);
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet s1 = book.CreateSheet("物料库存");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("物料名称");
            r1.CreateCell(1).SetCellValue("物料编码");
            r1.CreateCell(2).SetCellValue("物料类别");
            r1.CreateCell(3).SetCellValue("规格");
            r1.CreateCell(4).SetCellValue("单位");
            r1.CreateCell(5).SetCellValue("最小库存");
            r1.CreateCell(6).SetCellValue("最大库存");
            r1.CreateCell(7).SetCellValue("库存总数");
            r1.CreateCell(8).SetCellValue("单重");
            r1.CreateCell(9).SetCellValue("总重");
            for (int i = 0; i < datalist.Data.Count; i++)
            {
                IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(datalist.Data[i].Name);
                rt.CreateCell(1).SetCellValue(datalist.Data[i].Code);
                rt.CreateCell(2).SetCellValue(datalist.Data[i].TypeName);
                rt.CreateCell(3).SetCellValue(datalist.Data[i].Spec);
                rt.CreateCell(4).SetCellValue(datalist.Data[i].StorageName);
                rt.CreateCell(5).SetCellValue(Convert.ToDouble(datalist.Data[i].Min.ToString()));
                rt.CreateCell(6).SetCellValue(Convert.ToDouble(datalist.Data[i].Max.ToString()));
                rt.CreateCell(7).SetCellValue(Convert.ToDouble(datalist.Data[i].Totalinventory.ToString()));
                rt.CreateCell(8).SetCellValue(Convert.ToDouble(datalist.Data[i].Weight.ToString()));
                rt.CreateCell(9).SetCellValue(Convert.ToDouble(datalist.Data[i].GrossWeight.ToString()));
            }
            //输出的文件名称
            string fileName = "物料统计" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            book.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);

        }


    }
}